<template>
  <el-form ref="formRef" :inline="true" :model="form" :rules="rules"
           :label-width="options.labelWidth" :label-position="'top'">
    <el-row>
      <el-col :span="options.span" v-for="item in options.list">
        <el-form-item :label="item.label" :prop="item.prop"
                      v-show="item.visible==null?true:item.visible" :size="item.size||'small'">
          <!-- 文本框、数字框、下拉框、日期框、开关、上传 -->
          <el-input v-if="item.type === 'input'"
                    v-model="form[item.prop]"
                    :disabled="item.disabled"
                    type="textarea"
                    :title="form[item.prop]"
                    :placeholder="item.placeholder" clearable></el-input>

          <el-input v-if="item.type === 'textarea'" v-model="form[item.prop]" :disabled="item.disabled"
                    type="textarea"
                    :placeholder="item.placeholder" clearable></el-input>

          <el-input-number v-else-if="item.type === 'number'" v-model="form[item.prop]"
                           :disabled="item.disabled" controls-position="right"></el-input-number>
          <el-select v-else-if="item.type === 'select'" v-model="form[item.prop]" :disabled="item.disabled"
                     style="width: 165px"
                     v-show="item.visible"
                     :placeholder="item.placeholder">
            <el-option v-for="opt in item.opts" :label="opt.label" :value="opt.value">
            </el-option>

          </el-select>
          <!--          远程调用数据-->
          <el-select v-else-if="item.type === 'dynamic-select'" v-model="form[item.prop]" :disabled="item.disabled"
                     v-show="item.visible"
                     remote
                     remote-show-suffix
                     filterable
                     :remote-method="item.remoteMethod"
                     :loading="item.loading"
                     :placeholder="item.placeholder" clearable>
            <el-option v-for="opt in item.remoteOpts" :label="opt.label" :value="opt.value">
            </el-option>
          </el-select>

          <el-switch v-else-if="item.type === 'switch'"
                     :title="item.title"
                     v-model="form[item.prop]"
                     @change="item.callBack"
                     :active-value="item.activeValue" :inactive-value="item.inactiveValue"
                     :active-text="item.activeText" :inactive-text="item.inactiveText"
          ></el-switch>

          <el-upload v-else-if="item.type === 'upload'" class="avatar-uploader"

                     action="#"
                     @change="item.changeEvent"
                     v-show="item.visible"
                     :auto-upload="false"
                     :accept="item.fileAccept"
                     drag
                     limit="1"
                     :show-file-list="true" :on-success="handleAvatarSuccess">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传xlsx文件</div>
          </el-upload>

          <slot :name="item.prop" v-else>
          </slot>
        </el-form-item>
      </el-col>
    </el-row>

    <el-form-item>
      <div style="width: 100%;display: flex;justify-content: flex-end">
        <el-button type="primary" @click="saveEdit(formRef)">上传</el-button>
        <el-button type="info" @click="cancelOperation">取 消</el-button>
      </div>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>

import {FormOption} from "src/types/form-option";
import {ElMessage, FormInstance, FormRules, UploadProps} from 'element-plus';
import {PropType, ref} from 'vue';

const {options, formData, update, cancel} = defineProps({
  options: {
    type: Object as PropType<FormOption>,
    required: true
  },
  formData: {
    type: Object,
    required: true
  },
  update: {
    type: Function,
    required: true
  },
  cancel: {
    type: Function,
    required: true
  },
});

// status  默认开启按钮
const form = ref({...(formData)});
const rules: FormRules = options.list.map(item => {
  if (item.required != undefined) {
    return {[item.prop]: [{required: item.required, message: `${item.label}不能为空`, trigger: 'blur'}]};
  }
  return {};
}).reduce((acc, cur) => ({...acc, ...cur}), {});

const formRef = ref<FormInstance>();
const saveEdit = (formEl: FormInstance | undefined) => {
  if (!formEl) {
    return
  }
  formEl.validate(valid => {
    if (!valid) {
      ElMessage.warning('必填项不能为空，请检查');
      return false;
    }

    update(form.value);
  });
};

// 处理输入
const handleInput = (prop: string, value: any) => {
  console.log(form)
}
const cancelOperation = () => {
  cancel()
}

const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
  console.log(uploadFile)

  // form.value.thumb = URL.createObjectURL(uploadFile.raw!);
};

</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
